import { fromTemplate } from '$lib/builder';
import { Types, addComponent, defineComponent } from '$lib/ecs';
import { addToInventory } from '$lib/inventory';
import { machete } from './blades';
import { cottonScarf, fedora, hood, jeans, leatherGloves, leatherJacket, silentSneakers } from './clothes';
import { equip } from './equipment';
import { getSkill } from './skill';
import { lockpick, pocketKnife } from './tools';

const professions = [];

export const enumProfessions = () => professions;

export const defineProfession = (prof) => {
  professions.push(prof);
};

export const Profession = defineComponent('Profession', {
  profession: [Types.ui8, 32],
});

export const applyProfession = (world, pc, prof) => {
  addComponent(world, Profession, pc, {
    profession: prof.name,
  });
  for (let [tmplt, count] of prof.items ?? []) {
    for (let i = 0; i < (count ?? 1); ++i) {
      addToInventory(world, pc, fromTemplate(world, tmplt));
    }
  }
  for (let [slot, tmplt] of Object.entries(prof.equipped)) {
    equip(world, pc, slot, fromTemplate(world, tmplt));
  }
  for (let [skillName, amount] of Object.entries(prof.skills)) {
    const skill = getSkill(skillName);
    if (skill === undefined)
      console.warn('failed to find', skillName);
    addComponent(world, skill, pc, {
      amount
    });
  }
};

defineProfession({
  name: 'private investigator',
  description: "You discovered more than one secret and clinged to your old-school profession as long as you could",
  items: [
    [lockpick, 5],
  ],
  equipped: {
    head: fedora,
    hands: leatherGloves,
  },
  skills: {
    LockPicking: 1,
    Stealth: 1,
    Handguns: 1,
  },
});

defineProfession({
  name: 'thief',
  description: "You kept up good ol' stealing through the roughest times, but it's an uphill battle",
  items: [
    [lockpick, 5],
    [pocketKnife],
  ],
  equipped: {
    head: hood,
    hands: leatherGloves,
    feet: silentSneakers,
    legs: jeans,
    body: leatherJacket,
    neck: cottonScarf,
  },
  skills: {
    LockPicking: 1,
    Pickpocketing: 1,
    Stealth: 1,
  },
});

defineProfession({
  name: 'machete dancer',
  description: "You are one of the last people trained in a traditional dance of machete. What made you think this is viable career these days remains a question, but perhaps your skills might be repurposed",
  items: [
  ],
  equipped: {
    mainhand: machete,
  },
  skills: {
    MacheteDance: 2,
  },
});

/*

defineProfession({
  name: 'drug courier',
  description: "You used to deliver drugs discreetly and effectively, but couldn't survive competition from drones",
  items: [
    [joint],
    [amphetaminePill, 3],
  ],
  equipped: {
    feet: sneakers,
    legs: jeans,
    torso: leatherJacket,
  },
  skills: {
    stealth: 1,
    bartering: 1,
  },
});

defineProfession({
  name: 'barista',
  description: "Coffee is your passion that you enjoyed sharing with strangers.",
  items: [
    [eliteCoffeeBeans, 5],
  ],
  equipped: {
    
  },
  skills: {
    bartering: 1,
  },
});
*/
